<template>
  <div class="dictAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div class="search-button" v-if="!contactAddShow">
      <el-button
        v-if="pageType !== 'detail'"
        class="btn save"
        @click="handleAddSave(dictFormRef)"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >保存</el-button
      >
      <el-button class="btn direction" @click="handleBack"
        ><el-icon class="icon" color="#fff"> <Back /> </el-icon
        >返回</el-button
      >
    </div>
    <el-collapse v-model="activeNames">
      <!-- 基础信息 -->
      <el-collapse-item
        name="1"
        v-if="!contactAddShow"
        :class="contactAddShow ? 'customInfoUpClass' : 'customInfoDownClass'"
      >
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title"
            >{{
              pageType === "detail" ? "" : pageType === "add" ? "新增" : "编辑"
            }}字典信息</span
          >
        </template>
        <div class="dict-info">
          <el-form
            :model="dictForm"
            ref="dictFormRef"
            :rules="queryRules"
            label-width="100"
            v-enterToNext
            :disabled="pageType === 'detail'"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item label="字典名称" prop="dictName">
                  <el-input
                    ref="refInput"
                    v-model="dictForm.dictName"
                    placeholder="请输入字典名称"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="字典类型" prop="dictType">
                  <el-input
                    v-model="dictForm.dictType"
                    placeholder="请输入字典类型"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="状态" prop="status">
                  <el-radio-group v-model="dictForm.status">
                    <el-radio
                      v-for="dict in sys_normal_disable"
                      :key="dict.value"
                      :label="dict.value"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                  <el-input
                    v-model="dictForm.remark"
                    type="textarea"
                    maxlength="2000"
                    placeholder="请输入内容"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import { listType, getType, addType, updateType } from "@/api/system/dict/type";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
import { getDictsApi } from "@/api/dict";
import { ElMessage, ElMessageBox } from "element-plus";
const emit = defineEmits([
  "dictAddShow",
  "update:dictAddShow",
  "updateEvaluateAddShow",
  "refreshList",
]);
const props = defineProps({
  pageType: {
    type: String,
    default: "add",
  },
});
const activeNames = ref(["1", "2", "3"]);
const dictFormRef = ref();
const detailInfoShow = ref(false);
const dictOptions = ref([]);

const refInput = ref();
const dictFormTemplate = {
  dictId: undefined,
  dictName: undefined,
  dictType: undefined,
  status: "0",
  remark: undefined,
};
const data = reactive({
  dictForm: JSON.parse(JSON.stringify(dictFormTemplate)),
  queryRules: {
    dictName: [
      { required: true, message: "字典名称不能为空", trigger: "blur" },
    ],
    dictType: [
      { required: true, message: "字典类型不能为空", trigger: "blur" },
    ],
  },
  contactTypeList: [],
  templateList: [],
  provinceList: [],
  cityList: [],
  areaList: [],
  userList: [],
  dictList: [],
});
const { dictForm, queryRules, templateList } = toRefs(data);
const contactAddShow = ref(false);

/** 查询菜单下拉树结构 */
function getTreeselect() {
  dictOptions.value = [];
  listType().then((response) => {
    const dict = { dictId: 0, dictName: "主类目", children: [] };
    dict.children = proxy.handleTree(response.data, "dictId");
    dictOptions.value.push(dict);
  });
}

// 保存
const handleAddSave = async (dictFormRef) => {
  if (!dictFormRef) return;
  await dictFormRef.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定保存以上数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          if (dictForm.value.dictId != undefined) {
            updateType(dictForm.value).then((res) => {
              emit("update:dictAddShow", false);
              emit("refreshList", true);
              ElMessage.success(res.msg);
              dictForm.value = JSON.parse(JSON.stringify(dictForm));
            });
          } else {
            addType(dictForm.value).then((res) => {
              emit("update:dictAddShow", false);
              emit("refreshList", true);
              ElMessage.success(res.msg);
              dictForm.value = JSON.parse(JSON.stringify(dictForm));
            });
          }
        })
        .catch(() => {});
    } else {
      console.log("error submit!");
    }
  });
};

// 返回
const handleBack = () => {
  if (props.pageType === "detail") {
    emit("updateEvaluateAddShow", false);
    emit("update:dictAddShow", false);
    dictForm.value = JSON.parse(JSON.stringify(dictFormTemplate));
  } else {
    ElMessageBox.confirm("确定返回上一级吗?以上数据未保存将会丢失!", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      confirmButtonClass: "confirmBtnClass",
      cancelButtonClass: "cancelBtnClass",
    })
      .then(async () => {
        emit("updateEvaluateAddShow", false);
        emit("update:dictAddShow", false);
        dictForm.value = JSON.parse(JSON.stringify(dictFormTemplate));
      })
      .catch(() => {});
  }
};

async function getDicts() {
  await getDictsApi("contract_template_type").then((res) => {
    // 模板
    if (res.code === 200) {
      templateList.value = res.data;
    }
  });
}

// 处理父id
const handleParentId = (id) => {
  if (id) {
    dictForm.value.parentId = id;
  } else {
    dictForm.value.parentId = 0;
  }
};

// 处理详情信息
const handleDetailInfo = async (dictId) => {
  try {
    detailInfoShow.value = true;
    getType(dictId).then((response) => {
      dictForm.value = response.data;
      detailInfoShow.value = false;
    });
  } catch (error) {
    detailInfoShow.value = false;
  }
};

onMounted(() => {
  getDicts();
  getTreeselect();
  // getDictTreeselect();
  // if (props.pageType === "add") {
  nextTick(() => {
    refInput.value.focus(); // 聚焦
  });
  // }
});

defineExpose({
  handleDetailInfo,
  handleParentId,
});
</script>
<style lang="scss" scoped>
:deep(.el-collapse-item__header) {
  background-color: #f8faff;
}
.dictAdd {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  :deep(.el-date-editor.el-input, .el-date-editor.el-input__wrapper) {
    width: 268px !important;
    height: 34px !important;
  }
  /* .search-button {
    position: absolute;
    top: -45px;
    right: 60px;
    display: flex;
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;
      height: 34px;
      padding: 10px;
      // margin-right: 20px;
      font-size: 14px;
      border-radius: 4px;
      user-select: none;
      cursor: pointer;
    }
    .btn.save {
      color: #fff;
      background: rgba(67, 207, 124, 1);
      border: none;
      &:hover {
        background: rgba(67, 207, 124, 0.85);
      }
    }

    .btn.add {
      .btn.add {
        border: none;
        color: #fff;
        background: rgba(255, 159, 0, 1);
        &:hover {
          background: rgba(255, 159, 0, 0.85);
        }
      }
    }

    .btn.back {
      border: none;
      color: #fff;
      background: rgba(255, 159, 0, 1);
      &:hover {
        background: rgba(255, 159, 0, 0.85);
      }
    }
    .btn.direction {
      border: none;
      color: #fff;
      background: rgba(153, 153, 153, 1);
      &:hover {
        background: rgba(153, 153, 153, 0.85);
      }
    }

    .icon {
      margin-right: 6px;
    }
  } */
  .circle {
    margin: 0 10px 0 20px;
    width: 6px;
    height: 6px;
    background: #007cef;
    border-radius: 50%;
  }
  .collapse-title {
    font-weight: bold;
    font-size: 16px;
  }
  .el-collapse,
  .el-collapse-item__wrap {
    border: none;
  }

  .search-input {
    width: 268px;
    height: 34px;
  }
  .dict-info {
    padding: 20px 10px 5px 10px;
    background-color: #fafafa;
    margin: 10px 0;
  }
  .contact-info {
    padding-top: 20px;
    .table-operate {
      display: flex;
      justify-content: space-around;
      .table-icon {
        width: 20px;
        height: 20px;
        cursor: pointer;
      }
    }
  }
  .button-position {
    top: 6px;
    right: 30px;
  }
  :deep(.el-input__inner) {
    height: 34px;
  }
  .customInfoUpClass {
    -webkit-animation: customInfoDown 0.8s ease-out;
  }
  .customInfoDownClass {
    -webkit-animation: customInfoDown 0.8s ease-out;
  }

  .contactInfoUpClass {
    -webkit-animation: contactInfoUp 0.8s ease-out;
  }
  .contactInfoDownClass {
    -webkit-animation: contactInfoUp 0.8s ease-out;
  }
  @-webkit-keyframes customInfoUp {
    0% {
      opacity: 0;
      -webkit-transform: translateY(80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
  @-webkit-keyframes customInfoDown {
    0% {
      opacity: 0;
      -webkit-transform: translateY(-80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
  @-webkit-keyframes contactInfoUp {
    0% {
      opacity: 0;
      -webkit-transform: translateY(80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
}
</style>
